home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 746 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  39 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: Problem with stringco
  5. X-Nntp-Posting-Host: golden.ripco.com
  6. Message-ID: <DKw0DA.I0G@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Tue, 9 Jan 1996 00:07:58 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. amundt@pvv.unit.no (Amund Tveit)
  13. in <yewvimppjz5.fsf@hyll.idt.unit.no> wrote:
  14.  
  15. >You can try something like this :
  16. >char *Stringcopy(char *source, int startpos, size_t length)
  17. >{
  18. >        char tmpstring[SIZEBIGENOUGH];
  19. >        strncpy((char *)(source+startpos),tmpstring,length);
  20. >        return tmpstring;
  21. >}
  22.  
  23. >This function is untried but I'll guess it will work (after maybe some
  24. >change in the casting).
  25.  
  26. This is not a good idea.  Even after changing the strncpy call (assuming
  27. string.h has been included and that SIZEBIGENOUGH is) to
  28.  
  29.         strncpy(tmpstring, source+startpos, length);
  30.  
  31. this code will still not work.  You are attempting to return a local
  32. automatic array.  tmpstring will point to memory no longer allocated,
  33. and quite possibly corrupted.
  34.  
  35.     
  36. --
  37. * Martin Ambuhl       net: mambuhl@ripco.com
  38. * Chicago, IL (USA)    
  39.